package cn.tedu.myboot28.controller;

import cn.tedu.myboot28.entity.User;
import cn.tedu.myboot28.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.jws.soap.SOAPBinding;
import javax.servlet.http.HttpSession;

@RestController
public class UserController {
    @Autowired(required = false)
    UserMapper mapper;
    @RequestMapping("/reg")
    public int reg(User user){
        User u=mapper.selectByUsername(user.getUsername());
        if (u!=null){
            return 2;
        }
        mapper.insert(user);
        return 1;
    }
    @RequestMapping("/login")
    public int login(User user, HttpSession session){

        User u=mapper.selectByUsername(user.getUsername());
        if (u!=null){
            if (u.getPassword().equals(user.getPassword())) {
                session.setAttribute("user",u);
                return 1;

            }else return 3;
        }
        return 2;
    }
    @RequestMapping("/checkLogin")
    public User checkLogin(HttpSession session){
        return (User) session.getAttribute("user");
    }
    @RequestMapping("/logout")
    public void logout(HttpSession session){
        session.removeAttribute("user");
    }
}
